package cn.jaye.fabric.application;

import cn.jaye.fabric.application.command.InfoCommodityCommand;
import cn.jaye.fabric.common.constant.MicroConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.jaye.fabric.application.command.CreateCommodityCommand;
import cn.jaye.fabric.application.command.ListCommodityCommand;
import cn.jaye.fabric.domain.service.RestTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author Jaye
 * @create 2018-02-08 16:05
 */
@Service
public class CommodityApplicationService {

    @Autowired
    private RestTemplateService restTemplateService;

    @Autowired
    private MicroConstant microConstant;

    @Autowired
    private RestTemplate restTemplate;

    private static final String APIPACKAGR_SERVICE_NAME = "commodity";


    public String listApiPackage(ListCommodityCommand listCommodityCommand) {
        String url = microConstant.getApiGatewayAddress() + APIPACKAGR_SERVICE_NAME + "/list";
        JSONObject postData = new JSONObject();
        JSONObject pageData = new JSONObject();
        if (listCommodityCommand.getSizes() != null) {
            pageData.put("sizes", listCommodityCommand.getSizes());
        }
        if (listCommodityCommand.getCurrentPage() != null) {
            pageData.put("currentPage", listCommodityCommand.getCurrentPage());
        }
        postData.put("page", pageData);
        if (StringUtils.isNotBlank(listCommodityCommand.getCondition())) {
            postData.put("commodityName", listCommodityCommand.getCondition());
        }
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return JSON.toJSONString(resultObject);
    }

    public String createCommodity(CreateCommodityCommand createCommodityCommand) {
        String url = microConstant.getApiGatewayAddress() + APIPACKAGR_SERVICE_NAME + "/create";
        JSONObject postData = new JSONObject();
        postData.put("commodityName", createCommodityCommand.getCommodityName());
        postData.put("commodityDescription", createCommodityCommand.getCommodityDescription());
        postData.put("price", createCommodityCommand.getPrice());
        postData.put("term", createCommodityCommand.getTerm());
        postData.put("apiPackage", createCommodityCommand.getAddApis());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return JSON.toJSONString(resultObject);
    }
    public String getCommodity(InfoCommodityCommand infoCommodityCommand) {
        String url = microConstant.getApiGatewayAddress() + APIPACKAGR_SERVICE_NAME + "/info";
        JSONObject postData = new JSONObject();
        postData.put("commodityId", infoCommodityCommand.getCommodityId());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return JSON.toJSONString(resultObject);
    }
}
